<?php
/***
 * 发货通知单队列
*/
namespace App\Jobs;

use App\Services\Orders\OrderService;
use App\Services\Warehouse\ConsignmentNoteService;
use App\Services\Warehouse\OrderGoodsInventoryCarryingDetailService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use PHPUnit\Exception;
use Symfony\Component\Translation\Exception\NotFoundResourceException;

class ConsignmentNoteQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $ConsignmentNoteService;
    protected $JobData;
    protected $OrderGoodsInventoryCarryingDetailService;
    public function __construct(array $JobData)
    {
        $this->JobData=$JobData;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(ConsignmentNoteService $consignmentNoteService,OrderGoodsInventoryCarryingDetailService $orderGoodsInventoryCarryingDetailService,OrderService $orderService)
    {
            $returnCNdata=$consignmentNoteService->SaveConsignmentNote($this->JobData);
            if(!empty($returnCNdata['Id'])){
                $ordergoods=$this->JobData['GoodsData'];
                if($ordergoods){
                    //订单绑定金蝶发货通知单编号
                    $orderService->UpdateOrderk3cn_sn($this->JobData['order_id'],$returnCNdata['Number']);
                    foreach ($ordergoods as $k=>$v){
                        $updata=[
                            'order_id'=>$this->JobData['order_id'],
                            'goods_sn'=>$v['goods_sn'],
                            'consignment_note_sn'=>$returnCNdata['Number'],
                            'goods_number'=>$v['goods_number']
                        ];
                        $orderGoodsInventoryCarryingDetailService->UpdateOGICData($updata);
                        unset($updata);
                    }
                    $data=[
                        'order_id'=>$this->JobData['order_id'],
                        'o_status'=>2
                    ];
                    $orderGoodsInventoryCarryingDetailService->UpdateO_statusBatch($data);
                }else{
                    $data=[
                        'order_id'=>$this->JobData['order_id'],
                        'o_status'=>3
                    ];
                    $orderGoodsInventoryCarryingDetailService->UpdateO_statusBatch($data);
                    return false;
                }
            }else{
                $data=[
                    'order_id'=>$this->JobData['order_id'],
                    'o_status'=>4
                ];
                $orderGoodsInventoryCarryingDetailService->UpdateO_statusBatch($data);
                return false;
            }
    }
}
